/*
 * @Descripttion:  
 * @version:  
 * @Author: chenxf
 * @Date: 2022-05-06 10:24:53
 */
import { getAction, deleteAction, putAction, postAction } from '@/api/manage'
import Vue from 'vue'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'
import { serviceApi } from '@/api/common.js'

export const api = {

  list:serviceApi.dict + '/dicts',
  delete: (ids) => `${serviceApi.dict}/dicts/${ids}`,
  exportXlsUrl: serviceApi.dict + '/dicts/exportXls',
  importExcelUrl: serviceApi.dict + '/dicts/importExcel',
  refreshCache:  serviceApi.dict +'/dicts/refresh-cache',
  queryAllDictItems:  serviceApi.dict +'/dicts/list-all-dict-item',
  deleteList:  serviceApi.dict +'/dicts/list-delete'
}

/**
 * 刷新缓存
 * @param parameter
 * @returns {*}
 */
export const refreshDictCache = params => getAction(api.refreshCache, params);

/**
 * 获取所有的字典Item列表
 * @param {*} params
 * @returns
 */
export const queryDictItems = params => getAction(api.queryAllDictItems, params);

/**
 * 获取已删除的列表数据
 * @param {*} params
 * @returns
 */
export const getDeleteList = params => getAction(api.deleteList, params);

/**
 * 字典取回
 * @param {*} id
 * @returns
 */
export const backDictById = (id) => putAction(`${serviceApi.dict}/dicts/back/${id}`);

/**
 * 彻底删除
 * @param {*} id
 * @returns
 */
export const deletePhysicById = (id) => deleteAction(`${serviceApi.dict}/dicts/delete-physic/${id}`);

/**
 * 添加字典
 * @param {*} params
 * @returns
 */
export const addDict = params => postAction(serviceApi.dict + '/dicts', params)
/**
 * 编辑字典
 * @param {} params
 * @returns
 */
export const editDict = params => putAction(serviceApi.dict + '/dicts', params)

/**
 * 逻辑删除
 * @param id
 * @returns {AxiosPromise}
 */
export const deleteDict = (ids, params) => deleteAction(api.delete(ids), params);

/**
 * 字典标签专用（通过code获取字典数组）
 * @param {*} code
 * @param {*} params
 * @returns
 */
export const getDictItemsByCode = (code, params) => getAction(`${serviceApi.dict}/dicts/dict-item/${code}`, params)

/**
 * 从缓存中获取字典配置
 * @param {*} dictCode
 * @returns
 */
export const getDictItemsFromCache = (dictCode) => {
  if (Vue.ls.get(UI_CACHE_DB_DICT_DATA) && Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
    let dictItems = Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]
    console.log('-----------getDictItemsFromCache----------dictCode=' + dictCode + '---- dictItems=', dictItems)
    return dictItems
  }
}
